package cn.maxwell._02_xhr;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

import java.nio.charset.StandardCharsets;

/**
 * 异步消息生产者
 */
public class Producer {
    public static void main(String[] args) throws Exception{
        //1.创建生产者对象
        DefaultMQProducer producer = new DefaultMQProducer("xhr-producer-group");
        //2.设置namesrv地址
        producer.setNamesrvAddr("localhost:9876");
        //3.开启生产者实例
        producer.start();
        //4.设置消息
        Message message = new Message("xhr", "max", "万家灯火故事长！".getBytes(StandardCharsets.UTF_8));
        //5.发送消息
        producer.send(message, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println(sendResult);
            }

            @Override
            public void onException(Throwable throwable) {
                System.out.println("消息接收失败"+throwable.getMessage());
            }
        });
        //6.关闭实例（异步请求需要设置定时关闭）
//        producer.shutdown();
    }
}
